<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="关键点" prop="keyPoint">
            <el-input v-model="form.keyPoint" placeholder="请输入关键点"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="专业名称" prop="zymcId">
            <el-select v-model="form.zymcId" filterable  style="width: 240px" @change="handleNew" :disabled="isallName">
              <el-option v-for="item in options"  :key="item.zymcId" :label="item.zymcName" :value="item.zymcId"/>
            </el-select>
          </el-form-item>
          </el-col>
        <el-col :span="24" class="mb20">
            <el-form-item label="专业等级" prop="zydjId">
                <el-select v-model="form.zydjId" filterable  style="width: 240px" :disabled="isallName">
                    <el-option v-for="item in zydjnamelist"  :key="item.zydjId" :label="item.zydjName" :value="item.zydjId"/>
                </el-select>
            </el-form-item>
          </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
          </el-form-item>
          </el-col>
       </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="JnglKeyPointsDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj} from '/@/api/train/jnglKeyPoints'
import { rule } from '/@/utils/validate';
import { professionalnameList,getLevel } from '/@/api/train/jnglProfessionLevel';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const options =reactive([{
	zymcName: '',
	zymcId: ''
}])
const zydjnamelist = reactive([{
	zydjName: '',
	zydjId: ''
}])
// 提交表单数据
const form = reactive({
      id:'',
      zydjName: '',
      remark: '',
      zymcId: '',
      keyDescription: '',
	    keyPoint: '',
      zydjId:'',
      zydjIds: '',
      zymcIds: ''
});
// 禁用下拉框
const isallName = ref(false)
// 定义校验规则
const dataRules = ref({
	keyPoint: [{ required: true, message: '请输入关键点', trigger: 'blur' },],
	zydjId: [{ required: true, message: '请选择专业等级', trigger: 'blur' },],
	zymcId: [{ required: true, message: '请选择专业名称', trigger: 'blur', },],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''
  isallName.value = false
  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取jnglKeyPoints信息
  if (id) {
    form.id = id
    getjnglKeyPointsData(id)
  }
    // 专业名称下拉框
  professionalnameList().then((res) => {
		options.length = 0;
		for (let index = 0; index < res.data.length; index++) {
			options.push({
				zymcName: res.data[index].zymcName,
				zymcId: res.data[index].zymcId,
			});
		}
		console.log("查询分类121===>",options);
	});

};
// 专业等级
const handleNew = async () => {
	console.log(form.zymcId,"zymcId");
	var zid = form.zymcId;
	getLevel(zid).then((res) => {
			zydjnamelist.length = 0;
		for (let index = 0; index < res.data.length; index++) {
					zydjnamelist.push({
				zydjName: res.data[index].zydjName,
				zydjId: res.data[index].zydjId
			});
		}
		console.log("查询分类===>",zydjnamelist);
	})
}

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;
  if(form.id){
    form.zydjId = form.zydjIds
    form.zymcId = form.zymcIds
  }

	try {
    loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getjnglKeyPointsData = (id: string) => {
  // 获取数据
  loading.value = true
  isallName.value = true
  getObj(id).then((res: any) => {
    console.log(res.data,11111111111)
    Object.assign(form, res.data[0])
    form.zydjId = res.data[0].zydjName
    form.zymcId = res.data[0].zymcName
    form.zydjIds = res.data[0].zydjId
    form.zymcIds = res.data[0].zymcId
    console.log(form,22222222222)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
